<template>
  <div class="hospital-top">
    <div class="top-content">
      <div class="top-left" @click="goBackHome">
        <img src="../assets/images/logo.png" alt="logo">
        <p>小医通预约挂号统一平台</p>
      </div>
      <div class="top-right">
        <el-color-picker
            v-model="color"
            size="small"
            show-alpha
            :predefine="predefineColors"
            @change="handleColorChange"
        />
        <el-switch v-model="switchValue" class="mt-2" inline-prompt @change="handleSwitchChange"
                   :active-icon="Moon" :inactive-icon="Sunny"/>
        <el-button type="info" plain>帮助中心</el-button>
        <el-dropdown>
          <el-button type="primary" v-if="!userStore.userInfo.token" @click="handleLogin">
            登录/注册
            <el-icon class="el-icon--right">
              <arrow-down/>
            </el-icon>
          </el-button>
          <el-button type="primary" plain v-else>
            {{ userStore.userInfo.name }}
          </el-button>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="goUser('/user/rename')">实名认证</el-dropdown-item>
              <el-dropdown-item @click="goUser('/user/orders')">挂号订单</el-dropdown-item>
              <el-dropdown-item @click="goUser('/user/patients')">就诊人管理</el-dropdown-item>
              <el-dropdown-item @click="goLoginOut">退出登录</el-dropdown-item>
              <el-dropdown-item>修改密码</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>

<script setup>
import {ArrowDown, Moon, Sunny} from '@element-plus/icons-vue'
import {useRouter} from 'vue-router';
// 引入用户仓库
import {useUserStore} from '@/store/modules/user.js'

const userStore = useUserStore()

// 引入主题色
import {ref} from 'vue'

const color = ref('#1890ff')
const predefineColors = [
  '#ff4500',
  '#ff8c00',
  '#ffd700',
  '#90ee90',
  '#00ced1',
  '#1e90ff',
  '#c71585',
  'rgba(255, 69, 0, 0.68)',
  'rgb(255, 120, 0)',
  'hsv(51, 100, 98)',
  'hsva(120, 40, 94, 0.5)',
  'hsl(181, 100%, 37%)',
  'hsla(209, 100%, 56%, 0.73)',
  '#c7158577'
]
const switchValue = ref(false)
const drawer = ref(false)

const handleColorChange = () => {
  // 修改根节点的样式
  const root = document.querySelector('html')
  root.style.setProperty('--el-color-primary', color.value)
}
const handleSwitchChange = () => {
  // 获取html根节点
  const root = document.querySelector('html')
  // 切换深色模式
  if (switchValue.value) {
    root.classList.add('dark')
    root.classList.remove('light')
  } else {
    root.classList.add('light')
    root.classList.remove('dark')
  }
}


const router = useRouter();
const goBackHome = () => {
  router.push({path: '/home'});
}

// 登录/注册弹窗
const handleLogin = () => {
  userStore.dialogFormVisible = true
}

// 实名认证
const goUser = (path) => {
  router.push({path: path})
}
// 退出登录
const goLoginOut = () => {
  userStore.logout()
  router.push({path: '/home'})
}


</script>

<style scoped lang="scss">
.hospital-top {
  width: 100%;
  height: 70px;
  background-color: #ffffff;
  box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.2);
  display: flex;
  position: fixed;
  z-index: 999;

  .top-content {
    display: flex;
    justify-content: space-between;
    width: 1200px;
    height: 100%;
    margin: 0 auto;
    padding: 0 20px;

    .top-left {
      display: flex;
      align-items: center;
      cursor: pointer;

      img {
        width: 60px;
        height: 60px;
        margin-right: 10px;
      }

      p {
        font-size: 24px;
        font-weight: bold;
        color: #24bfee;

      }
    }

    .top-right {
      display: flex;
      align-items: center;

      .mt-2 {
        margin: 0 10px;
      }

      button {
        margin: 10px;
      }
    }
  }
}

</style>
